<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评论时间">
        <el-date-picker
          v-model="daterangeProductTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="评论评分">
        <el-rate
          v-model="queryParams.productScore"
          :colors="colors"
          style="padding-top: 6.5px;">
        </el-rate>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:productComment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:productComment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productCommentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="评论内容" align="center" prop="commentContent" />
      <el-table-column label="评论时间" align="center" prop="productTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="productScore" label="评分" align="center" width="150">
        <template v-slot="scope">
          <el-rate
            v-model="scope.row.productScore"
            :colors="colors"
            disabled
            >
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column label="评论图片" align="center" prop="commentImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.commentImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column prop="commentVideo" label="评论视频" align="center" width="300">
        <template v-slot="scope">
            <video controls width="300" height="100" :src="scope.row.commentVideo"></video>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" prop="sysUser.nickName" />
      <el-table-column label="订单商品" align="center" prop="productDetail.productName" />
      <el-table-column label="订单规格" align="center" prop="productOrder.productType.typeName" />
      <el-table-column label="状态" align="center" prop="del">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.del"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:productComment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[4, 8, 20, 30]"
      @pagination="getList"
    />

    <!-- 添加或修改商品评论对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProductComment, getProductComment, delProductComment, addProductComment, updateProductComment } from "@/api/product/productComment";
export default {
  name: "ProductComment",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      //星星颜色
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品评论表格数据
      productCommentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态时间范围
      daterangeProductTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productTime: null,
        productScore: null,
        userId: null,
        orderId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品评论列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeProductTime && '' != this.daterangeProductTime) {
        this.queryParams.params["beginProductTime"] = this.daterangeProductTime[0];
        this.queryParams.params["endProductTime"] = this.daterangeProductTime[1];
      }
      listProductComment(this.queryParams).then(response => {
        this.productCommentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        commentContent: null,
        productTime: null,
        productScore: null,
        commentImg: null,
        commentVideo: null,
        userId: null,
        orderId: null,
        del: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      const queryParamsToSend = {};
      queryParamsToSend.params={};
      // 处理时间范围条件
      if (null!= this.daterangeProductTime && ''!= this.daterangeProductTime) {
          queryParamsToSend.params["beginProductTime"] = this.daterangeProductTime[0];
          queryParamsToSend.params["endProductTime"] = this.daterangeProductTime[1];
      }
      // 处理评分条件，只有当评分有值时才添加到发送参数中
      if (this.queryParams.productScore!== null) {
          queryParamsToSend["productScore"] = this.queryParams.productScore;
      }
      // 其他可能的查询条件也按类似逻辑按需添加，比如用户编号、订单编号等
      // 此处假设暂不处理它们，如有需要可类似上述方式添加判断添加
      console.log("即将发送给后端的查询参数：", queryParamsToSend.params);
      listProductComment(queryParamsToSend).then(response => {
          this.productCommentList = response.rows;
          this.total = response.total;
          this.loading = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeProductTime = [];
      this.queryParams={
        pageNum: 1,
        pageSize: 10,
        productTime: null,
        productScore: null,
        userId: null,
        orderId: null,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品评论";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductComment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品评论";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductComment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductComment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品评论编号为"' + ids + '"的数据项？').then(function() {
        return delProductComment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/productComment/export', {
        ...this.queryParams
      }, `productComment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
  /* 星星样式 */
  .star-rating {
      display: flex;
      align-items: center;
  }
  .filled-star {
      color: gold; /* 设置填充星星的颜色为金色，类似金星颜色，可以根据喜好调整 */
      font-size: 20px; /* 根据需要调整星星图标大小 */
      margin-right: 5px; /* 调整星星之间的间距 */
  }
  .empty-star {
      color: lightgray; /* 设置未填充星星的颜色 */
      font-size: 20px;
      margin-right: 5px;
  }
  .star-rating-table {
      display: flex;
      align-items: center;
  }
  .star-rating-table.filled-star {
      color: gold;
      font-size: 16px; /* 可根据表格内整体效果适当调整图标大小，此处示例设为16px */
      margin-right: 3px; /* 调整星星之间间距 */
  }
  .star-rating-table.empty-star {
      color: lightgray;
      font-size: 16px;
      margin-right: 3px;
  }
</style>
